# -*-* encoding:UTF-8 -*-
# author            : mengy
# date              : 2019/2/19
# python-version    : Python 3.7.0
# description       : 文章操作相关接口


import json
from flask import Flask, jsonify, request
from com.articles.microapp.service.remarks_service import RemarkService
from com.articles.microapp.utils.logger_utils import Logger
from com.articles.microapp.utils.constants import CommonConstants
from com.articles.microapp.config.config import CONFIG


class RemarkController(object):
    consts = CommonConstants()
    config = CONFIG.config()

    logger = Logger()

    remarks_service = RemarkService()

    def __init__(self, app=Flask(__name__)):
        app.add_url_rule("/getRemarks", view_func=self.get_remarks, methods=['GET'])
        app.add_url_rule("/addRemark", view_func=self.add_remark, methods=['POST'])
        app.add_url_rule("/like", view_func=self.like, methods=['GET'])
        app.add_url_rule("/cancelLike", view_func=self.cancel_like, methods=['GET'])

    def get_remarks(self):
        """
        获取所有评论信息
        :return:
        """
        article_id = request.args.get('articleId')
        args1 = request.args.get('pageNo')
        page_no = 1 if not args1 or int(args1) < 1 else int(args1)
        page_size = 0 if not request.args.get('pageSize') else int(request.args.get('pageSize'))
        user_id = '28045612534'
        if page_size != 0:
            start = (page_no - 1) * page_size
            remarks = self.remarks_service.get_remarks(article_id, user_id, page=[start, page_size])
        else:
            remarks = self.remarks_service.get_remarks(article_id, user_id)
        return jsonify(remarks)

    def add_remark(self):
        """
        添加评论信息 \n
        :return:
        """
        data = request.data
        if data:
            args = json.loads(data)
            article_id = args['articleId']
            content = args['content']
            user_id = '28045612534'
            return jsonify(self.remarks_service.add_remark(article_id, user_id, content))
        else:
            return jsonify(False)

    def like(self):
        """
        点赞 \n
        :return:
        """
        id = request.args.get('objId')
        user_id = "28045612534"
        return jsonify(self.remarks_service.add_like(id, user_id))

    def cancel_like(self):
        """
        取消点赞 \n
        :return:
        """
        id = request.args.get('objId')
        user_id = "28045612534"
        return jsonify(self.remarks_service.cancel_like(id, user_id))
